/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xuidemo.slice;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.eventhandler.EventRunner;

import com.xuexiang.xuidemo.MainAbility;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.util.RPResultListener;
import com.xuexiang.xuidemo.util.RuntimePermissionUtil;
import com.xuexiang.xuidemo.util.Shareprefance;
import com.xuexiang.xuidemo.util.WeakHandler;

/**
 * Logo页面
 *
 * @since 2021-03-24
 */
public class LogoAbility extends Ability {
    private static final int TWOZEROZEROZERO = 2000;
    private static final int ONEZEROZERO = 100;
    EventRunner runner = EventRunner.current();
    private WeakHandler handler = new WeakHandler(runner);
    /**
     * task
     */
    private final Runnable task = new Runnable() {
        @Override
        public void run() {
            Shareprefance shareprefance = new Shareprefance(getContext()); // 判断是否是第一次进来，是否是有登录过
            shareprefance.initDB();
            if (shareprefance.select(shareprefance.startFlag)) {
                if (shareprefance.select(shareprefance.loginFlag)) {
                    Intent intent = new Intent(); // 登陆过，进入登入页查询密码账号是否正确（现在没这个功能），直接进入首页
                    intent.setOperation(new Intent.OperationBuilder()
                        .withBundleName(getContext().getBundleName())
                        .withAbilityName(MainLoadAbility.class)
                        .build());
                    startAbility(intent);
                    terminateAbility();
                } else {
                    boolean isSkipLogin = false; // BuildConfig.DEBUG;
                    Intent intent = new Intent();
                    intent.setOperation(new Intent.OperationBuilder()
                        .withBundleName(getContext().getBundleName())
                        .withAbilityName(isSkipLogin ? MainLoadAbility.class : LoginAbility.class)
                        .build());
                    startAbility(intent);
                    terminateAbility();
                }
            } else {
                shareprefance.insert(shareprefance.startFlag,
                    true); // 没有开启过.withAbilityName("com.xuexiang.xuidemo.MainAbility")
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                    .withBundleName(getContext().getBundleName())
                    .withAbilityName(MainAbility.class)
                    .build();
                intent.setOperation(operation);
                startAbility(intent);
                terminateAbility();
            }
        }
    };

    /**
     * onStart
     *
     * @param intent
     */
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_logo_layout);
        handler.postDelayed(task, TWOZEROZEROZERO);
    }

    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == ONEZEROZERO) {
            RuntimePermissionUtil.onRequestPermissionsResult(grantResults, new RPResultListener() {
                @Override
                public void onPermissionGranted() {
                }

                /**
                 * onPermissionDenied
                 */
                @Override
                public void onPermissionDenied() {
                    terminateAbility();
                }
            });
        }
    }
}
